package com.woniu.coupon.config;

/**
 * @author 杨光 微信号:tolryg
 * @time 17:00
 */


import com.ruoyi.common.constant.UserConstants;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 杨光 微信号:tolryg
 * @time 14:54
 */
@Configuration
public class RabbitConfig {


//    //延时交换机
//    public static final String DELAY_EXCHANGE = "woniu63.spring.boot.delay.exchange";//
//    //死信交换机
//    public static final String DLX_EXCHANGE = "woniu63.spring.boot.dlx.exchange";
//
//    //延时队列
//    public static final String DELAY_QUEUE="woniu63.spring.boot.delay.queue";
//    //死信队列
//    public static final String DLX_QUEUE="woniu63.spring.boot.dlx.queue";
//    //延时队列routingkey
//    public static final String DELAY_ROUTING_KEY="woniu63.spring.boot.delay.routingkey";
//    //死信队列routingkey
//    public static final String DLX_ROUTING_KEY="woniu63.spring.boot.dlx.routingkey";


    //延时交换机
    @Bean("directExchange")
    public DirectExchange directExchange(){
        return new DirectExchange(UserConstants.DELAY_CUSTOMER_COUPON_EXCHANGE);
    }


    //死信交换机
    @Bean("dlxExchange")
    public DirectExchange dlxExchange(){
        return new DirectExchange(UserConstants.DLX_CUSTOMER_COUPON_EXCHANGE);
    }

    //延时队列
    @Bean("delayQueue")
    public Queue delayQueue(){
        Map<String,Object> arguments= new HashMap();
        arguments.put("x-message-ttl",20000);//消息在队列中停一天 1000*60*60*24
        arguments.put("x-dead-letter-exchange",UserConstants.DLX_CUSTOMER_COUPON_EXCHANGE);//死信交换机名称
        arguments.put("x-dead-letter-routing-key", UserConstants.DLX_CUSTOMER_COUPON_KEY);//死信routing key

        return QueueBuilder.nonDurable(UserConstants.DELAY_CUSTOMER_COUPON_QUEUE).withArguments(arguments).build();
    }

    //死信队列
    @Bean("dlxQueue")
    public Queue dlxQueue(){
        return QueueBuilder.nonDurable(UserConstants.DLX_CUSTOMER_COUPON_QUEUE).build();
    }

    @Bean
    public Binding delayQueueBinding(
            @Qualifier("delayQueue") Queue queue,
            @Qualifier("directExchange") DirectExchange exchange
    ){
        return BindingBuilder.bind(queue).to(exchange).with(UserConstants.DELAY_CUSTOMER_COUPON_KEY);

    }

    @Bean
    public Binding dlxQueueBinding(
            @Qualifier("dlxQueue") Queue queue,
            @Qualifier("dlxExchange") DirectExchange exchange
    ){
        return BindingBuilder.bind(queue).to(exchange).with(UserConstants.DLX_CUSTOMER_COUPON_KEY);

    }
}
